//package com.plian.system.service.sys.warning.impl;
//
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.plian.Tools.CollectionUtil;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.config.exception.MyRuntimeException;
//import com.plian.system.dao.bt.baseinfo.BaseInfoDao;
//import com.plian.system.dao.fr.authorize.BudgetAuthorizeDao;
//import com.plian.system.dao.fr.budgetadjustment.BudgetAdjustmentDao;
//import com.plian.system.dao.fr.financeindicators.FinanceIndicatorsDao;
//import com.plian.system.dao.fr.payrolladjustment.PayrollAdjustmentDao;
//import com.plian.system.dao.sys.excel.AutoExcelDao;
//import com.plian.system.dto.sys.warning.WarningFactorDTO;
//import com.plian.system.entity.sys.warning.WarningFactor;
//import com.plian.system.entity.sys.warning.WarningFactorExtension;
//import com.plian.system.mapper.sys.warning.WarningFactorMapper;
//import com.plian.system.model.ObjResult;
//import com.plian.system.model.page.Page;
//import com.plian.system.mybatis.support.Condition;
//import com.plian.system.service.fr.adjustment.IAdjustmentTableVersionService;
//import com.plian.system.service.fr.authorize.IAuthorizeTableVersionService;
//import com.plian.system.service.fr.payrolladjustment.IPayrollVersionService;
//import com.plian.system.service.fr.report.IReportVersionService;
//import com.plian.system.service.sys.warning.IWarningFactorExtensionService;
//import com.plian.system.service.sys.warning.IWarningFactorService;
//import com.plian.system.vo.fr.bulletin.BulletinTypeVO;
//import com.plian.system.vo.sys.warning.AutoExcelInfoVO;
//import com.plian.system.vo.sys.warning.SheetInfoVO;
//import com.plian.system.vo.sys.warning.WarningFactorVO;
//import com.plian.system.wrapper.sys.warning.WarningFactorExtensionWrapper;
//import com.plian.system.wrapper.sys.warning.WarningFactorWrapper;
//import lombok.AllArgsConstructor;
//import org.springframework.stereotype.Service;
//
//import java.util.Comparator;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
//import static com.plian.system.constant.sys.WarningDataSourceConstant.*;
//
//@Service
//@AllArgsConstructor
//public class WarningFactorServiceImpl extends ServiceImpl<WarningFactorMapper, WarningFactor> implements IWarningFactorService {
//
//    private IWarningFactorExtensionService warningFactorExtensionService;
//    private WarningFactorWrapper warningFactorWrapper;
//    private WarningFactorExtensionWrapper warningFactorExtensionWrapper;
//    private IReportVersionService reportVersionService;
//    private FinanceIndicatorsDao financeIndicatorsDao;
//    private IPayrollVersionService payrollVersionService;
//    private PayrollAdjustmentDao payrollAdjustmentDao;
//    private IAuthorizeTableVersionService authorizeTableVersionService;
//    private BudgetAuthorizeDao budgetAuthorizeDao;
//    private IAdjustmentTableVersionService adjustmentTableVersionService;
//    private BudgetAdjustmentDao budgetAdjustmentDao;
//    private AutoExcelDao autoExcelDao;
//    private BaseInfoDao baseInfoDao;
//
//
//    private static final String VERSION = "version";
//    private static final String YEAR = "year";
//    private static final String EXCEL_CODE = "code";
//
//    private static final String SHEET_ID = "orgId";
//    private static final String SHEET_ID2 = "formId";
//    private static final String SHEET_NAME = "name";
//    private static final String TIME_FLAG = "timeFlag";
//    private static final String AUTO_EXCEL_NAME = "name";
//    private static final String START_TIME = "startTime";
//
//    @Override
//    public WarningFactorVO detail(WarningFactor warningFactor) {
//        WarningFactorVO warningFactorVO = warningFactorWrapper.entityToVO(getOne(Condition.getQueryWrapper(warningFactor)));
//        if (warningFactorVO == null){
//            throw new MyRuntimeException(new ObjResult("该数据不存在"));
//        }
//        warningFactorVO.setWarningFactorExtensionVOList(warningFactorExtensionWrapper
//                .entityToVO(warningFactorExtensionService.list(Wrappers.<WarningFactorExtension>lambdaQuery()
//                        .eq(WarningFactorExtension::getFactorId, warningFactor.getId()))));
//
//        return warningFactorVO;
//    }
//
//    @Override
//    public boolean submit(WarningFactorVO warningFactorVO) {
//        WarningFactor warningFactor = warningFactorWrapper.voToEntity(warningFactorVO);
//        List<WarningFactorExtension> warningFactorExtensionList = warningFactorExtensionWrapper.voToEntity(warningFactorVO.getWarningFactorExtensionVOList());
//        boolean flag = saveOrUpdate(warningFactor);
//        warningFactorExtensionService.remove(Wrappers.<WarningFactorExtension>lambdaQuery().eq(WarningFactorExtension::getFactorId, warningFactor.getId()));
//        if (CollectionUtil.isNotEmpty(warningFactorExtensionList)){
//            for (WarningFactorExtension warningFactorExtension : warningFactorExtensionList){
//                warningFactorExtension.setFactorId(warningFactor.getId());
//            }
//            warningFactorExtensionService.saveBatch(warningFactorExtensionList);
//        }
//        return flag;
//    }
//
//    @Override
//    public boolean removeAndChild(WarningFactorDTO warningFactorDTO) {
//        warningFactorExtensionService.remove(Wrappers.<WarningFactorExtension>lambdaQuery()
//                .in(WarningFactorExtension::getFactorId, warningFactorDTO.getIds()));
//        return removeByIds(warningFactorDTO.getIds());
//    }
//
//    @Override
//    public List<SheetInfoVO> sheetList(WarningFactorDTO warningFactorDTO) {
//        switch (warningFactorDTO.getDataSource()){
//            case BUDGET:
//                PageData budgetPd = new PageData();
//                budgetPd.put(VERSION, reportVersionService.getVersion(warningFactorDTO.getYear()));
//                try {
//                    List<PageData> budgetList = financeIndicatorsDao.findbudgettype(budgetPd);
//                    if (CollectionUtil.isNotEmpty(budgetList)){
//                       return budgetList.stream().map(budgetSheet -> {
//                            return SheetInfoVO.builder()
//                                    .sheetId(budgetSheet.getString(SHEET_ID)).sheetName(budgetSheet.getString(SHEET_NAME)).build();
//                        }).collect(Collectors.toList());
//                    }
//                } catch (Exception e) {
//                    throw new RuntimeException(e);
//                }
//                break;
//            case SALARY_BUDGET:
//                PageData payrollPd = new PageData();
//                payrollPd.put(VERSION, payrollVersionService.getVersion(warningFactorDTO.getYear()));
//                try {
//                    List<PageData> payrollAdjustmentList = payrollAdjustmentDao.listAllOrg(payrollPd);
//                    if (CollectionUtil.isNotEmpty(payrollAdjustmentList)){
//                        return payrollAdjustmentList.stream().map(budgetSheet -> {
//                            return SheetInfoVO.builder()
//                                    .sheetId(budgetSheet.getString(SHEET_ID)).sheetName(budgetSheet.getString(SHEET_NAME)).build();
//                        }).collect(Collectors.toList());
//                    }
//                } catch (Exception e) {
//                    throw new RuntimeException(e);
//                }
//                break;
//            case APPROVAL:
//                PageData authorizePd = new PageData();
//                authorizePd.put(VERSION, authorizeTableVersionService.getVersion(warningFactorDTO.getYear()));
//                try {
//                    List<PageData> authorizeList = budgetAuthorizeDao.listAllOrg(authorizePd);
//                    if (CollectionUtil.isNotEmpty(authorizeList)){
//                        return authorizeList.stream().map(budgetSheet -> {
//                            return SheetInfoVO.builder()
//                                    .sheetId(budgetSheet.getString(SHEET_ID)).sheetName(budgetSheet.getString(SHEET_NAME)).build();
//                        }).collect(Collectors.toList());
//                    }
//                } catch (Exception e) {
//                    throw new RuntimeException(e);
//                }
//                break;
//            case BUDGET_ADJUSTMENT:
//                PageData budgetAdjustmentPd = new PageData();
//                budgetAdjustmentPd.put(VERSION, adjustmentTableVersionService.getVersion(warningFactorDTO.getYear()));
//                try {
//                    List<PageData> budgetAdjustmentList = budgetAdjustmentDao.listAllOrg(budgetAdjustmentPd);
//                    if (CollectionUtil.isNotEmpty(budgetAdjustmentList)){
//                        return budgetAdjustmentList.stream().map(budgetSheet -> {
//                            return SheetInfoVO.builder()
//                                    .sheetId(budgetSheet.getString(SHEET_ID)).sheetName(budgetSheet.getString(SHEET_NAME)).build();
//                        }).collect(Collectors.toList());
//                    }
//                } catch (Exception e) {
//                    throw new RuntimeException(e);
//                }
//                break;
//            case EXCEL:
//                PageData excelPd = new PageData();
//                excelPd.put(YEAR, warningFactorDTO.getYear());
//                excelPd.put(EXCEL_CODE, warningFactorDTO.getExcelCode());
//                PageData excel = null;
//                try {
//                    excel = autoExcelDao.findByCode(excelPd);
//                    excel.put("excelId", excel.getString("id"));
//                    List<PageData> formList = autoExcelDao.findByExcelId(excel);
//                    if (CollectionUtil.isNotEmpty(formList)){
//                        return formList.stream().map(budgetSheet -> {
//                            return SheetInfoVO.builder()
//                                    .sheetId(budgetSheet.getString(SHEET_ID2)).sheetName(budgetSheet.getString(SHEET_NAME)).build();
//                        }).collect(Collectors.toList());
//                    }
//                } catch (Exception e) {
//                    throw new RuntimeException(e);
//                }
//
//
//        }
//        return null;
//    }
//
//    @Override
//    public List<AutoExcelInfoVO> autoExcelList() {
//        Page page = new Page(false, null);
//        try {
//            List<PageData> list = autoExcelDao.listAutoExcel(page);
//            if (CollectionUtil.isEmpty(list)){
//                return null;
//            }
//            List<AutoExcelInfoVO> results = list.stream().filter(pd -> pd.getInteger(TIME_FLAG) == 1)
//                    .map(pd -> AutoExcelInfoVO.builder()
//                            .excelCode(pd.getString(EXCEL_CODE))
//                            .name(pd.getString(AUTO_EXCEL_NAME))
//                            .build())
//                    .collect(Collectors.toList());
//            Map<String, List<PageData>> excelCodeMap = list.stream().filter(pd -> pd.getInteger(TIME_FLAG) == 2)
//                    .collect(Collectors.groupingBy(pd -> pd.getString(EXCEL_CODE)));
//            if (CollectionUtil.isEmpty(excelCodeMap)){
//                return results.stream().sorted(Comparator.comparing(AutoExcelInfoVO::getExcelCode)).collect(Collectors.toList());
//            }
//            for (String excelCode : excelCodeMap.keySet()){
//                PageData tempPd = excelCodeMap.get(excelCode).stream()
//                        .sorted(Comparator.comparing(pd -> pd.getDate(START_TIME))).collect(Collectors.toList()).get(0);
//                results.add(AutoExcelInfoVO.builder()
//                        .excelCode(tempPd.getString(EXCEL_CODE))
//                        .name(tempPd.getString(AUTO_EXCEL_NAME))
//                        .build());
//            }
//            return results.stream().sorted(Comparator.comparing(AutoExcelInfoVO::getExcelCode)).collect(Collectors.toList());
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    @Override
//    public List<BulletinTypeVO> bulletinCodeList(String year) {
//        try {
//            return baseInfoDao.findBulletin(year);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
//}
